;(function(root){
    //默认参数
    var defaults = {
        selector: '.lazyload',
        delay: 200,
        src: 'data-src'
    }

    //内部函数，合并对象
    var extend = function(){
        var results = {},
            i = 0,
            deep = false,
            length = arguments.length;
        if(typeof arguments[0] === "boolean"){
            i = 1;
            deep = arguments[0];
        }
        while(i<length){
            loop(arguments[i]);
            i++;
        }
        function loop(obj){
            for(var k in obj){
                if(deep && Object.prototype.toString.call(obj[k]) === '[object Object]'){
                    results[k] = extend(true, obj[k]);
                }else{
                    results[k] = obj[k];
                }
            }
        }
        return results;
    }

    //节流函数（页面滚动时使用，节省性能开销）
    var throttle = function(fn, wait){
        var last = Date.now();
        return function(){
            var self = this;
            var now = Date.now();
            if(now - last >= wait){
                fn.apply(self, arguments);
                last = now;
            }
        }
    }

    var Lazyload = function(options){
        this.settings = extend(defaults, options || {});
        this.images = document.querySelectorAll(this.settings.selector);
        this.length = this.images.length;
        this.clientHeight = this.getClientHeight();
        this.finish = false;
        this.init();
    }

    Lazyload.prototype = {
        init: function(){
            var self = this;
            
            self.ifInCurrentScreen();

            self.addEventListener(root, 'scroll', throttle(function(){
                !self.finish && self.ifInCurrentScreen();
            }, self.settings.delay))

            self.addEventListener(root, 'resize', throttle(function(){
                self.clientHeight = self.getClientHeight();
                !self.finish && self.ifInCurrentScreen();
            }, self.settings.delay))
        },
        //判断图片是否处于当前视口
        ifInCurrentScreen: function(){
            var self = this;
            for(var i = 0; i < self.images.length; i++){
                var this_img = self.images[i];
                var rect = this_img.getBoundingClientRect();
                if(rect.bottom >= 0 && rect.top <= self.clientHeight) {
                  self.showPic(this_img);
                }
            }
        },
        //监听事件方法（为了兼容ie6,7,8）
        addEventListener: function(element, event, callback){
            root.addEventListener? element.addEventListener(event, callback) : root.attachEvent? element.attachEvent('on'+event, callback) : element['on'+event] = callback;
        },
        //当图片都显示完毕，执行该方法
        stop: function(){
            this.finish = true;
        },
        //显示图片
        showPic: function(image){
            var self = this;
            var dataSrc = image.getAttribute(self.settings.src);
            var src = image.getAttribute('src');
            if(dataSrc != src){
                image.src = dataSrc;
                self.length -= 1;
                !self.length && self.stop();
            }
        },
        //获取页面视口高度（不包含滚动条）
        getClientHeight: function(){
            return root.innerHeight || document.documentElement.clientHeight;
        }
    }

    root.lazyload = function(options){
        //设置一个定时器，30ms后执行，为了兼容IE（假如页面停在中间，刷新页面，IE浏览器回先回到顶部，然后再跳到中间位置，如果你用IE不显示图片，请把定时器的时间调大点）
        setTimeout(function(){
            return new Lazyload(options);
        }, 30)
    }
    
}(window));